package com.itboye.jigongbao.activity

import android.view.View
import android.widget.RelativeLayout
import com.itboye.jigongbao.R
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.retrofit.base.ResultEntity
import com.itboye.jigongbao.retrofit.presenter.UserPrestener
import com.itboye.jigongbao.retrofit.presenter.UserPrestener.By_SecurityCode_send_Fail
import com.itboye.jigongbao.retrofit.presenter.UserPrestener.By_Suggest_add_Success
import com.itboye.jigongbao.utils.TitleBarUtils
import com.itboye.jigongbao.utils.ToastUtil.ByAlert
import kotlinx.android.synthetic.main.activity_advice.*
import java.util.*

class AdviceActivity : BaseActivity(), Observer {
    override fun update(p0: Observable?, p1: Any?) {

        var entity: ResultEntity = p1 as ResultEntity
        if (entity.code != 0) {
            ByAlert.alert(p1.msg)
        } else {
            when (entity.eventType) {
                By_Suggest_add_Success -> {
                    ByAlert.alert(entity.msg)
                    finish()
                }
                By_SecurityCode_send_Fail -> {
                    ByAlert.alert(entity.msg)
                }
            }
        }
    }

    private var userPresenter: UserPrestener? = null
    private var lay_actionbar_left: RelativeLayout? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_advice
    }

    override fun initData() {
        TitleBarUtils().initTitlebarStyle1(this, this!!.trans_action_bar_advice!!, intent.getStringExtra("title"), R.drawable.img_back, "", 0, "")
        userPresenter = UserPrestener(this)
    }

    override fun onClick(p0: View?) {
        when (p0) {
            lay_actionbar_left -> {
                finish()
            }
            else -> {
            }
        }
    }

    fun submitMyAdvice(p0: View?) {
        var advice = ed_advice.text.toString()
        if ("" == advice) {
            ByAlert.alert("请填写建议后再提交哦!")
            return
        }
        when (advice.length > 250) {
            true -> {
                ByAlert.alert("字数不能超过250个字哦!")
            }
            else -> {
                userPresenter?.By_Suggest_add(MyApplication!!.instance!!.loginInfo!!.getUid().toString(), advice, this)
            }
        }
    }
}
